package com.example.javatest.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.javatest.entity.domain.InfoSystem;
import com.example.javatest.entity.domain.TbUser;
import com.example.javatest.entity.domain.request.UserLoginRequest;
import com.example.javatest.entity.domain.request.UserRegisterRequest;
import com.example.javatest.service.InfoSystemService;
import com.example.javatest.service.TbUserService;
import com.example.javatest.utils.RoleCheck;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Resource
    private TbUserService tbUserService;

    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    public int userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if(userRegisterRequest == null){
            return -1;
        }
        String userName = userRegisterRequest.getUserName();
        String userPwd = userRegisterRequest.getUserPwd();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if(StringUtils.isAnyBlank(userName,userPwd,checkPassword)){
            return -1;
        }
        int id = tbUserService.userRegister(userName, userPwd, checkPassword);
        return id;
    }

    /**
     * 用户登录
     * @param userLoginRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/login")
    public TbUser userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest httpServletRequest){
        if(userLoginRequest == null){
            return null;
        }
        String userName = userLoginRequest.getUserName();
        String userPwd = userLoginRequest.getUserPwd();
        if(StringUtils.isAnyBlank(userName,userPwd)){
            return null;
        }
        return tbUserService.userLogin(userName,userPwd,httpServletRequest);
    }

    /**
     * 用户查询
     * @param userName
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/search")
    public List<TbUser> searchUsers(String userName,HttpServletRequest httpServletRequest){
        if (!RoleCheck.isAdmin(httpServletRequest)) {
            return new ArrayList<>();
        }
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(userName)){
            queryWrapper.like("user_Name",userName);
        }
        return tbUserService.list(queryWrapper);
    }

    /**
     * 用户删除（逻辑删除）
     * @param id
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/delete")
    public boolean deleteUserById(@RequestBody int id,HttpServletRequest httpServletRequest){
        if (!RoleCheck.isAdmin(httpServletRequest)) {
            return false;
        }
        if(id<=0){
            return false;
        }
        return tbUserService.removeById(id);
    }


}

